//
//  AppDelegate.m
//  UI13_UITabBarController
//
//  Created by TravelRound on 16/1/29.
//  Copyright © 2016年 TravelRound. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface AppDelegate () <UITabBarControllerDelegate>

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    // 创建3个界面, 每个界面有独立的NC去管理
    RootViewController *rootVC = [[[RootViewController alloc] init] autorelease];
    UINavigationController *rootNC = [[[UINavigationController alloc] initWithRootViewController:rootVC] autorelease];
    // 为每个界面设置自己的标题风格
    rootNC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:10001] autorelease];
    
    SecondViewController *secondVC = [[[SecondViewController alloc] init] autorelease];
    UINavigationController *secondNC = [[[UINavigationController alloc] initWithRootViewController:secondVC] autorelease];
    secondNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"咖啡" image:[UIImage imageNamed:@"100-coffee.png"] tag:10002] autorelease];
    
    
    ThirdViewController *thirdVC = [[[ThirdViewController alloc] init] autorelease];
    UINavigationController *thirdNC = [[[UINavigationController alloc] initWithRootViewController:thirdVC] autorelease];
    thirdNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"聊天" image:[UIImage imageNamed:@"09-chat2.png"] selectedImage:[UIImage imageNamed:@"08-chat.png"]] autorelease];
    thirdNC.tabBarItem.badgeValue = @"99+";
    
//    SecondViewController *secondVC1 = [[[SecondViewController alloc] init] autorelease];
//    UINavigationController *secondNC1 = [[[UINavigationController alloc] initWithRootViewController:secondVC1] autorelease];
//    secondNC1.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"咖啡" image:[UIImage imageNamed:@"100-coffee.png"] tag:10002] autorelease];
//    
//    SecondViewController *secondVC2 = [[[SecondViewController alloc] init] autorelease];
//    UINavigationController *secondNC2 = [[[UINavigationController alloc] initWithRootViewController:secondVC2] autorelease];
//    secondNC2.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"咖啡" image:[UIImage imageNamed:@"100-coffee.png"] tag:10002] autorelease];
//    
//    SecondViewController *secondVC3 = [[[SecondViewController alloc] init] autorelease];
//    UINavigationController *secondNC3 = [[[UINavigationController alloc] initWithRootViewController:secondVC3] autorelease];
//    secondNC3.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"咖啡" image:[UIImage imageNamed:@"100-coffee.png"] tag:10002] autorelease];
    
    // 高度49, 5个以上会重叠
    UITabBarController *tabBarC = [[UITabBarController alloc] init];
    // 重要属性 - 所管理的界面数组
//    tabBarC.viewControllers = @[rootNC, secondNC, thirdNC, secondNC1, secondNC2, secondNC3];
    tabBarC.viewControllers = @[rootNC, secondNC, thirdNC];

    tabBarC.delegate = self;
    
    // 风格属性
    // 边框颜色
    tabBarC.tabBar.tintColor = [UIColor greenColor];
    tabBarC.tabBar.barTintColor = [UIColor lightGrayColor];
    tabBarC.tabBar.backgroundColor = [UIColor blackColor];
    // 是否半透明
    tabBarC.tabBar.translucent = YES;
    // 背景图片
    [tabBarC.tabBar setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"]];
    
    self.window.rootViewController = tabBarC;
    [tabBarC release];
    
    
    return YES;
    
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"%ld", tabBarController.selectedIndex);
    // 选择清除提示"99+"
    if (2 == tabBarController.selectedIndex) {
        
        UINavigationController *navigationC = (UINavigationController *)viewController;
        navigationC.tabBarItem.badgeValue = nil;
        
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
